// pages/renzhen/baseinfo/baseinfo.js

var citylist = require('../../../utils/citylist.js').citylist
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    citylist: citylist,
    gender:0,
    date: '请选择生日',
    guxiang: ['北京市', '北京市', '东城区'],
    gongzuo: ['北京市', '北京市', '东城区'],
    guxiangstate: 0,
    gongzuostate: 0,
    customItem:'全部',
    disable:false
  },
  // 获取名字
  getName:function(e){
    var name = e.detail.value
    this.setData({
      name: name
    })
  },
  // 选择生日
  bindDate: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  constellation:function(e){
    this.setData({
      xingzuo: e.detail.value
    })
  },
  // 提示弹窗
  showmodal: function (es) {
    wx.showModal({
      title: '提示',
      content: es,
      showCancel: false,
      confirmColor: '#ff8e03'
    })
  },
  //显示省
  showPro: function (e) {
    var o = e.currentTarget.dataset
    this.setData({
      showPro: !this.data.showPro,
      protype: o.protype,
    })
  },
  // 选择省
  selectPro: function (e) {
    var o = e.currentTarget.dataset
    var citylist = this.data.citylist
    var protype = this.data.protype
    if (protype == 1){
      this.setData({
        proIndex: o.index,
        selectedPro1: citylist[o.index],
        selectedCity1: null
      })
    }else {
      this.setData({
        proIndex: o.index,
        selectedPro2: citylist[o.index],
        selectedCity2:null
        
      })
    }
  },
  //显示市
  showCity: function (e) {
    var o = e.currentTarget.dataset
    var selectedPro1 = this.data.selectedPro1
    var selectedPro2 = this.data.selectedPro2
    if (o.citytype == 1 && selectedPro1){
      this.setData({
        showCity: !this.data.showCity,
        citytype: o.citytype,
      })
    } else if (o.citytype == 2 && selectedPro2) {
      this.setData({
        showCity: !this.data.showCity,
        citytype: o.citytype,
      })
    } else {
      this.showmodal("您还没有选择省")
    }

  },
  // 选择市
  selectCity: function (e) {
    var citytype = this.data.citytype
    var o = e.currentTarget.dataset
    var selectedPro1 = this.data.selectedPro1
    var selectedPro2 = this.data.selectedPro2
    if (citytype == 1){
      this.setData({
        cityIndex1: o.index,
        selectedCity1: selectedPro1.children[o.index],
      })
    }else{
      this.setData({
        cityIndex2: o.index,
        selectedCity2: selectedPro2.children[o.index],
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDate()
    var gender = wx.getStorageSync('userInfo').gender
    this.setData({
      gender: gender
    })
  },
  // 生成日期
  getDate() {
    var _date = new Date();
    var endDate = _date.getFullYear() + '-' + (_date.getMonth() + 1) + '-' + _date.getDate()
    console.log(endDate)
    this.setData({
      endDate: endDate
    })
  },
  // 关闭选项
  close: function () {
    this.setData({
      showPro: 0,
      showCity: 0,
    })
  },
  summit:function(){
    var that = this 
    wx.showLoading({
      title: '正在保存',
      mask: true,
    })
    this.setData({
      disable: true
    })
    var data = {
      wxopenid:wx.getStorageSync('openId'),
      username:this.data.name, 
        sex:this.data.gender,     // 性别
        xingzu: this.data.xingzuo,  //星座
        birthday: this.data.date,   //生日
        province: this.data.selectedPro1.name,   //故乡省
        city: this.data.selectedCity1.name,           //故乡市
      province2: this.data.selectedPro2.name,   //现在工作省
      city2: this.data.selectedCity2.name,          //现工作市
     
    }
    wx.request({
      url: app.globalData.url+ 'Card/cardbase',
      data: data,
       success: function (res) {
        console.log(res)
        wx.navigateBack({
          delta: 1,
        })
      },
      complete:function(res){
        wx.hideLoading()
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getInfo()
  },
  // 加载页面信息
  getInfo: function () {
    var that = this
    wx.request({
      url: app.globalData.url + 'Card/auth',
      data: { wxopenid: wx.getStorageSync('openId') },
      success: function (res) {
        console.log(res)
        var userbase = res.data.userbase
        var selectedPro1 = {
          name: userbase.province
        }, selectedCity1 = {
          name: userbase.city
          }, selectedPro2 = {
            name: userbase.province2
        }, selectedCity2 = {
          name: userbase.city2
        }

        that.setData({
          name: userbase.username,
          xingzuo: userbase.xingzu,  //星座
          date: userbase.birthday,   //生日
          selectedPro1:selectedPro1,   //故乡省
          selectedCity1: selectedCity1,           //故乡市
          selectedPro2: selectedPro2,   //现在工作省
          selectedCity2: selectedCity2,          //现工作市
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})